/*----------------------------------------------------------------------------*\
| /gamemode/cl_fatigue.lua             | fatigue system                  | CLI |
\*----------------------------------------------------------------------------*/

local fatigue = {}

local meta = FindMetaTable( "Player" )

local MyFatigue = ZDayConfig["MaxFatigue"]

function meta:GetFatigue()
	return MyFatigue
end 

function fatigue.Update( um )
	MyFatigue = um:ReadShort()
end
usermessage.Hook( "SyncFatigue", function( um ) fatigue.Update( um ) end )

function fatigue.Tick()
		ply = LocalPlayer()
		
		if ( ply:KeyDown( IN_SPEED ) && ( ply:KeyDown( IN_FORWARD )	|| ply:KeyDown( IN_BACK ) || ply:KeyDown( IN_MOVELEFT ) || ply:KeyDown( IN_MOVERIGHT ) ) ) || ply:GetMoveType() == MOVETYPE_LADDER then	
			MyFatigue = MyFatigue - 2
		else
			MyFatigue = MyFatigue + 1
			if ply:Crouching() && ply:IsOnGround() then
				MyFatigue = MyFatigue + 1
			end
		end
		
		MyFatigue = math.Clamp( MyFatigue, 0, ZDayConfig["MaxFatigue"] )	

		if ply:GetMoveType() == MOVETYPE_LADDER && MyFatigue <= 5 then
			fatigue.FallOffLadder( ply )
		end		
end
timer.Create( "fatigue.Tick", 0.1, 0, fatigue.Tick )

function fatigue.FallOffLadder( ply )
	ply:ConCommand( "+jump" )
	timer.Simple( 0.5, function() ply:ConCommand( "-jump" ) end )
end 

--[[
function DoPlayerBindPress( ply, bind, pressed )
	if !ply:OnGround() && ply:GetMoveType() != MOVETYPE_LADDER then
		-- Disable the forward, left, back right and use binds if we are in the air and not on a ladder.
		if string.find( bind, "+jump" ) || string.find( bind, "+use" ) || string.find( bind, "+forward" ) || string.find( bind, "+back" ) || string.find( bind, "+moveleft" ) || string.find( bind, "+moveright" ) then
			return true 
		end
	end
end
hook.Add( "PlayerBindPress", "DoPlayerBindPressZday", DoPlayerBindPress )
]]-- This is shit and does not work, needs something...